package com.zhang.second.day03;

import com.zhang.second.utils.IntegerSource;
import com.zhang.second.utils.IntegerStatistic;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @title: 使用状态变量同时求最大，最小，平均值
 * @author: zhang
 * @date: 2022/1/30 15:43
 */
public class Example9 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new IntegerSource())
                .keyBy(r -> 1)
                .process(new KeyedProcessFunction<Integer, Integer, IntegerStatistic>() {
                    //声明值状态变量,每个key维护自己的状态变量，状态变量是一个单例模式，只能初始化一次
                    private ValueState<IntegerStatistic> accumulator;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        //初始化状态变量，首先去状态后端找对应的状态变量，如果找不到或初始化一个
                        //acc是状态变量在状态后端的名字
                        accumulator = getRuntimeContext().getState(
                                new ValueStateDescriptor<IntegerStatistic>(
                                        "acc",
                                        Types.POJO(IntegerStatistic.class)
                                )
                        );
                    }

                    @Override
                    public void processElement(Integer value, KeyedProcessFunction<Integer, Integer, IntegerStatistic>.Context ctx, Collector<IntegerStatistic> out) throws Exception {
                        if (accumulator.value() == null) {
                            accumulator.update(
                                    new IntegerStatistic(
                                            value,
                                            value,
                                            value,
                                            1,
                                            value
                                    )
                            );
                        } else {
                            IntegerStatistic tmp = accumulator.value();
                            accumulator.update(
                                    new IntegerStatistic(
                                            Math.max(value, tmp.max),
                                            Math.min(value, tmp.min),
                                            value + tmp.count,
                                            tmp.count + 1,
                                            (value + tmp.count) / (tmp.count + 1)
                                    )
                            );
                        }
                        out.collect(accumulator.value());
                    }
                })
                .print();

        env.execute();
    }
}
